package com.chinator.customComponents;

import java.util.ArrayList;

import com.chinator.chinator_v2.R;
import com.chinator.obs.Observable;
import com.chinator.obs.Observador;
import com.example.dataBase.FactoriaCiudad;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class CitySelector extends ImageView implements Observable  {
	
	View view;
	String ciudad;
	int bandera;
	Context _context;
	CitySelectorDialog dialog;
	
	public CitySelector(Context context){
		super(context);
		int padding_in_dp = 12;
		final float scale = getResources().getDisplayMetrics().density;
		int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
		this.setPadding(padding_in_px,padding_in_px,padding_in_px,padding_in_px);
		
		String ciudad = FactoriaCiudad.getCiudad(context);
	    if(ciudad!=null){
	    	this.chosen(getPosition(ciudad));
	    }
		
		this.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				openDialog();
			}
		});
	}
	
	public static int getPosition(String comunidad){
		int selection = -1;
		comunidad=comunidad.replace("_"," ");
    	for(int i = 0; i<CitySelectorDialog.CIUDADES.length; i++){
    		 if(CitySelectorDialog.CIUDADES[i].matches(comunidad)){
    			 selection = i; break;
    		 }
    	}
    	return selection;
	}
	
	public void openDialog() {
		dialog = new CitySelectorDialog(_context, this);
		dialog.show();
	}
	
	public void setContext(Context context){
		this._context = context;
	}
	
	public void chosen(int position){
		if(position!=-1)
			this.chosen(CitySelectorDialog.CIUDADES[position],CitySelectorDialog.BANDERAS[position]);
	}
	
	public void chosen(String ciudad, int bandera) {
		this.ciudad = ciudad;
		this.bandera = bandera;
		
		this.setImage(this.bandera);
		if(dialog!=null)
			dialog.dismiss();
		
		notificaObservadores();
	}
	
	public void chosen(View view) {
		this.view = view;
		this.ciudad = ((TextView)view.findViewById(R.id.textoColumna)).getText().toString();
		this.bandera = (Integer) ((ImageView)view.findViewById(R.id.imagenRow)).getTag();
		
		this.setImage(this.bandera);
		if(dialog!=null)
			dialog.dismiss();
		
		notificaObservadores();
	}
	
	public String getCiudad(){
		return ciudad;
	}
	
	private void setImage(int resource){
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),resource);
		bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth()*2, bitmap.getHeight()*2, false);
		
		/*Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);
		Paint paint = new Paint();
		        paint.setShader(shader);
		paint.setAntiAlias(true);
		Canvas c = new Canvas(circleBitmap);
		c.drawColor(Color.WHITE);
		c.drawCircle(bitmap.getWidth()/2, bitmap.getWidth()/2, bitmap.getHeight()/2, paint);*/
		
		this.setImageBitmap(addWhiteBorder(bitmap,10));
	}
	
	private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
	    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
	    Canvas canvas = new Canvas(bmpWithBorder);
	    canvas.drawColor(Color.WHITE);
	    canvas.drawBitmap(bmp, borderSize, borderSize, null);
	    return bmpWithBorder;
	}
	
  //################## Observable ####################
    
    private ArrayList<Observador> observadores;
    
	@Override
	public void addObservador(Observador obs) {
		if(observadores==null)
			observadores = new ArrayList<Observador>();
		
		this.observadores.add(obs);
	}

	@Override
	public void notificaObservadores() {
		if(observadores!=null)
		for(Observador obs : observadores)
			obs.actualizar();
	}
}
